package com.ossjk.qlh.system.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.ossjk.core.base.controller.BaseController;
import com.ossjk.core.util.CommonUtil;
import com.ossjk.core.util.EncryptUtil;
import com.ossjk.core.vo.RequestBean;
import com.ossjk.core.vo.ResponseBean;
import com.ossjk.qlh.loginfo.LogModule;
import com.ossjk.qlh.loginfo.LogOperation;
import com.ossjk.qlh.system.entity.User;
import com.ossjk.qlh.system.service.ILoginrecordService;
import com.ossjk.qlh.system.service.IPermissionService;
import com.ossjk.qlh.system.service.IUserService;
import com.ossjk.qlh.system.vo.LoginrecordVo;
import com.ossjk.qlh.system.vo.UserVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@LogModule(value = "首页")
@Api(tags = "首页")
@RestController
@RequestMapping("/main")
public class MainController extends BaseController {

	@Autowired
	private ILoginrecordService iLoginrecordService;
	@Autowired
	private IUserService iUserService;
	@Autowired
	private IPermissionService iPermissionService;

	/**
	 * 获取用户信息
	 * 
	 * @return
	 */
	@ApiOperation(value = "获取用户信息")
	@PostMapping(value = "/getInfo")
	public ResponseBean<Map<String, Object>> getInfo(@RequestBody RequestBean requestBean) {
		Map<String, Object> data = new HashMap<String, Object>();
		// 获取用户id
		String uid = getUid();
		// 获取用户信息
		UserVo userVo = iUserService.getInfoById(uid);
		data.put("user", userVo);
		QueryWrapper<LoginrecordVo> queryWrapper = new QueryWrapper<LoginrecordVo>();
		queryWrapper.orderByDesc("l.logintime");
		queryWrapper.eq("u.id", getUid());
		data.put("loginrecord", iLoginrecordService.pageVo(requestBean.getPage(), queryWrapper));
		return responseSuccess(data);
	}

	/**
	 * 去上传头像
	 * 
	 * @param id
	 * @return
	 */
	@LogOperation("去上传头像")
	@ApiOperation(value = "去上传头像")
	@PostMapping(value = "/toUploadHead")
	public ResponseBean<User> toUploadHead() {
		String uid = getUid();
		return responseSuccess(iUserService.getById(uid));
	}

	/**
	 * 上传头像
	 * 
	 * @param id
	 * @return
	 */
	@LogOperation("上传头像")
	@ApiOperation(value = "上传头像")
	@PostMapping(value = "/uploadHead")
	@ApiOperationSupport(includeParameters = { "head" })
	public ResponseBean uploadHead(@RequestBody RequestBean<User> requestBean) {
		User user = requestBean.getData();
		String uid = getUid();
		user.setId(uid);
		if (iUserService.updateById(user)) {
			return responseSuccess();
		} else {
			return responseFail();
		}
	}

	/**
	 * 编辑密码
	 * 
	 * @param id
	 * @return
	 */
	@LogOperation("编辑密码")
	@ApiOperation(value = "编辑密码")
	@PostMapping(value = "/updatePwd")
	@ApiImplicitParams({ @ApiImplicitParam(name = "oldPwd", value = "旧密码", required = true, paramType = "query"), @ApiImplicitParam(name = "newPwd", value = "新密码", required = true, paramType = "query") })
	public ResponseBean updatePwd(@RequestBody RequestBean<Map<String, String>> requestBean) {
		String uid = getUid();
		// 验证旧密码
		// 获取数据库的密码
		User user = iUserService.getById(uid);
		if (!CommonUtil.isEquals(EncryptUtil.MD5(requestBean.getData().get("oldPwd")), user.getPwd())) {
			return responseFail("旧密码错误");
		} else {
			// 旧密码正确
			// 编辑密码
			User user2 = new User();
			user2.setId(uid);
			user2.setPwd(EncryptUtil.MD5(requestBean.getData().get("newPwd")));
			if (iUserService.updateById(user2)) {
				return responseSuccess();
			} else {
				return responseFail();
			}
		}
	}

	/**
	 * 去编辑用户信息
	 * 
	 * @param id
	 * @return
	 */
	@LogOperation("去编辑用户信息")
	@ApiOperation(value = "去编辑用户信息")
	@PostMapping(value = "/toUpdateInfo")
	public ResponseBean<User> toUpdateInfo() {
		String uid = getUid();
		return responseSuccess(iUserService.getById(uid));
	}

	/**
	 * 编辑用户信息
	 * 
	 * @param id
	 * @return
	 */
	@LogOperation("编辑用户信息")
	@ApiOperation(value = "编辑用户信息")
	@PostMapping(value = "/updateInfo")
	public ResponseBean updateInfo(@RequestBody RequestBean<User> requestBean) {
		User user = requestBean.getData();
		String uid = getUid();
		user.setId(uid);
		if (iUserService.updateById(user)) {
			return responseSuccess();
		} else {
			return responseFail();
		}

	}
}
